<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020-08-14 16:27:12
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\ArticleModels;
use app\common\controller\AdminBaseController;
use Jrk\Tree;
use think\Exception;
use think\facade\Db;
use think\facade\Route;
use think\Request;
use app\admin\model\ArticleCates;
use app\admin\service\ExcelService;

class ArticleCate extends AdminBaseController
{

    protected function initialize()
   {
           parent::initialize(); // TODO: Change the autogenerated stub

           $this->model = new ArticleCates();

           $this->assign("models",ArticleModels::adminGetModelSelect());

           $this->assign("tree",Tree::toFormatTree($this->model->select()->toArray(), "name"));
    }



   /**
     * @param Request $request
     * @return string|\think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:添加
     */
    public function add(Request $request){
        $info=[];
        $this->assign(compact("info"));
        return $this->fetch();
    }


    /**
     * @param $id
     * @return string|\think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:编辑
     */
    public function edit($id){
        $info=$this->model->where("id",$id)->find()->toArray();
        if (!$info){
            return parent::failed("未查询到数据");
        }
        $this->assign(compact("info"));
        return $this->fetch("add");
    }

    
    public function ajaxGetCateModelid(){
        if ($this->request->isAjax()){
            $id = $this->request->get('id');
            $info = $this->model->where("id",$id)->field('model_id')->find()->toArray();
            if ($info) {
                $result = [
                    'msg' => '执行成功!',
                    'code' => 1,
                    'data' =>  $info['model_id']
                ];
            } else {
                $result = [
                    'msg' => '没有数据!',
                    'code' => 0,
                    'data' => []
                ];
            }
            return json($result);
        }
    }

    public function ajaxGetCatesByModelid(){
        if ($this->request->isAjax()){
            $model_id = $this->request->get('modelid');
            $cates = $this->model->where("model_id",$model_id)->select()->toArray();
            if ($cates) {
                $cates = Tree::toFormatTree($cates, "name");
                $result = [
                    'msg' => '执行成功!',
                    'code' => 1,
                    'data' =>  $cates
                ];
            } else {
                $result = [
                    'msg' => '没有数据!',
                    'code' => 0,
                    'data' => []
                ];
            }
            return json($result);
        }
    }


}